# List all coins
get_list_of_all_coinmarketcap_coins <- function() {
url = 'https://coinmarketcap.com/all/views/all/'
url_parsed <- htmlParse(getURL(url), asText = TRUE)
tableNodes <- getNodeSet(url_parsed, c('//*[@id="currencies-all"]'))
all_currency <- readHTMLTable(tableNodes[[1]])
currencies <- gsub("\n","", all_currency$Name)
library(stringr)
currencies <- str_replace(gsub("\\s+", " ", str_trim(currencies)), "B", "b")
c_symbols <- c()
c_names <- c()
for(c in currencies) {
cr <- strsplit(c, " ")
c_symbols <- c(c_symbols, cr[[1]][1])
c_names <- c(c_names, cr[[1]][1])
}
cr_df <- cbind(c_names, c_symbols)
colnames(cr_df) <- c('Name', 'Symbol')
return(as.data.frame(cr_df))
}
# Function to create url from start to end
get_currency_data_download_url <- function(currency, history_start, history_end) {
# Processing URL to download data
url_cmarket <- 'https://coinmarketcap.com/'
currency_part <- paste('currencies/', currency, sep="")
url_pre <- paste(url_cmarket, currency_part, sep="")
# Append start date
url_s <- paste(url_pre, '/historical-data/?start=', sep="")
url_s <- paste(url_s, history_start, sep="")
# Append end date
url_e <- paste(url_s, '&end=', sep="")
url_e <- paste(url_e, history_end, sep="")
url <- url_e
}
library(RCurl)
## Loading required package: bitops
library(XML)
library(bitops)
get_currency_historical_data <- function(url) {
url_parsed <- htmlParse(getURL(url), asText = TRUE)
tableNodes <- getNodeSet(url_parsed, c('//*[@class="table"]'))
currency_historical_data <- readHTMLTable(tableNodes[[1]])
return(currency_historical_data)
}
currency <- 'bitcoin'
history_start <- '20090101'
history_end <- '20171224'
url <- get_currency_data_download_url(currency, history_start, history_end)
data <- get_currency_historical_data(url)
# Get the data
date <- as.Date(rev(data$Date), "%B %d, %Y")
price_high <- as.numeric(as.character(rev(data$High)))
price_low <- as.numeric(as.character(rev(data$Low)))
price_open <- as.numeric(as.character(rev(data$Open)))
price_close <- as.numeric(as.character(rev(data$Close)))
vol <- as.numeric(gsub(",", "", as.character(rev(data$Volume))))
## Warning: NAs introduced by coercion
vol[is.na(vol)] <- 0
mc <- as.numeric(gsub(",", "", as.character(rev(data$`Market Cap`))))
mc[is.na(mc)] <- 0
# plotting
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
data$Date <- rev(date)
p <- data %>%
plot_ly(x = ~Date, type="ohlc",
open = ~Open, close = ~Close,
high = ~High, low = ~Low) %>%
layout(title = "Bitcoin Historical Price")
p
plot(date, price_high, cex=0.05)
lines(date, price_high)
lines(date, price_low, col='green')
points(date, price_low, col='green', cex=0.05)
lines(date, price_open, col='red')
points(date, price_open, col='red', cex=0.05)
lines(date, price_close, col='blue')
points(date, price_close, col='blue', cex=0.05)
currency <- 'zcoin'
history_start <- '20090101'
history_end <- '20171224'
url <- get_currency_data_download_url(currency, history_start, history_end)
data <- get_currency_historical_data(url)
# Get the data
date <- as.Date(rev(data$Date), "%B %d, %Y")
price_high <- as.numeric(as.character(rev(data$High)))
price_low <- as.numeric(as.character(rev(data$Low)))
price_open <- as.numeric(as.character(rev(data$Open)))
price_close <- as.numeric(as.character(rev(data$Close)))
vol <- as.numeric(gsub(",", "", as.character(rev(data$Volume))))
vol[is.na(vol)] <- 0
mc <- as.numeric(gsub(",", "", as.character(rev(data$`Market Cap`))))
## Warning: NAs introduced by coercion
mc[is.na(mc)] <- 0
# plotting
library(plotly)
library(quantmod)
plot_title <- paste(currency, "Historical Price", sep=" ")
data$Date <- rev(date)
p <- data %>%
plot_ly(x = ~Date, type="ohlc",
open = ~Open, close = ~Close,
high = ~High, low = ~Low) %>%
layout(title = plot_title)
p
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.